var user = document.getElementsByClassName('user')[0]
var pwd = document.getElementsByClassName('pwd')[0]
var phone = document.getElementsByClassName('phone')[0]
var email = document.getElementsByClassName('email')[0]
var submit = document.getElementsByClassName('submit')[0]


user.onblur = async function () {
    var userVal = user.value
    var tipsSpan = this.nextElementSibling
    var lReg = /^.{6,12}$/
    var sReg = /^[^0-9]/
    var zReg = /[^\w$]/
    if (!lReg.test(userVal)) {
        tipsSpan.textContent = "用户名需要在6-12位之间"
        tipsSpan.className = "user_tips error"
    } else if (!sReg.test(userVal)) {
        tipsSpan.textContent = "用户名不能以数字开头"
        tipsSpan.className = "user_tips error"
    } else if (zReg.test(userVal)) {
        tipsSpan.textContent = "用户名由数字、字母、下划线、$组成"
        tipsSpan.className = "user_tips error"
    } else {
        var { status, message } = await isExistUser({ user: userVal })
        if (status) {
            tipsSpan.textContent = message
            tipsSpan.className = "user_tips right"
            isUserOk = true
        } else {
            tipsSpan.textContent = message
            tipsSpan.className = "user_tips error"
        }
    }
}

pwd.onblur = function () {
    var pwdVal = pwd.value
    var reg = /^[\w$]{6,12}$/
    var tipsSpan = this.nextElementSibling
    if (reg.test(pwdVal)) {
        var isExistNum = /[0-9]/.test(pwdVal)
        var isExistBig = /[A-Z]/.test(pwdVal)
        var isExistSmall = /[a-z]/.test(pwdVal)
        var isExistSpe = /[_$]/.test(pwdVal)
        var level = isExistNum + isExistBig + isExistSmall + isExistSpe
        switch (level) {
            case 1:
                tipsSpan.textContent = "密码强度:弱"
                break
            case 2:
                tipsSpan.textContent = "密码强度:中"
                break
            case 3:
                tipsSpan.textContent = "密码强度:强"
                break
            case 4:
                tipsSpan.textContent = "密码强度:超强"
                break
        }
        tipsSpan.className = "pwd_tips right"
    } else {
        tipsSpan.textContent = "密码由数字、字母、下划线、$组成,6-12位"
        tipsSpan.className = "pwd_tips error"
    }
}

phone.onblur = async function () {
    var phoneVal = phone.value
    var reg = /^1[3-9]\d{9}$/
    var tipsSpan = this.nextElementSibling
    if (reg.test(phoneVal)) {
        var { status, message } = await isExistPhone({ phone: phoneVal })
        if (status) {
            tipsSpan.textContent = message
            tipsSpan.className = "phone_tips right"
            isUserOk = true
        } else {
            tipsSpan.textContent = message
            tipsSpan.className = "phone_tips error"
        }
    } else {
        tipsSpan.textContent = "请输入正确的手机号"
        tipsSpan.className = "phone_tips error"
    }
}

email.onblur = async function () {
    var emailVal = email.value
    var reg = /^\w+@\w+\.\w+$/
    var tipsSpan = this.nextElementSibling
    if (reg.test(emailVal)) {
        var { status, message } = await isExistEmail({ email: emailVal })
        if (status) {
            tipsSpan.textContent = message
            tipsSpan.className = "email_tips right"
            isUserOk = true
        } else {
            tipsSpan.textContent = message
            tipsSpan.className = "email_tips error"
        }
    } else {
        tipsSpan.textContent = "邮箱格式有误"
        tipsSpan.className = "email_tips error"
    }
}

submit.onclick = async function () {
    var userVal = user.value
    var pwdVal = pwd.value
    var phoneVal = phone.value
    var emailVal = email.value
    var { status, message } = await register({ user: userVal, pwd: pwdVal, phone: phoneVal, email: emailVal })
    if (status) {
        alert(message)
        setTimeout(() => {
            location.href = "./login.html"
        }, 1000)
    } else {
        alert(message)
    }
}